<template>
  <div class="canlendar-right">
    <div class="btn-group">
      <div @click="handlePrev"> <el-icon>
          <ArrowLeft />
        </el-icon></div>
      <p> {{ currDate }}</p>
      <div @click="handleNext"><el-icon>
          <ArrowRight />
        </el-icon></div>
    </div>
    <div class="week-header">
      <div v-for="item in weekData" :key="item">
        {{ item }}
      </div>
    </div>
    <div class="week-day">
      <div class="day" v-for="item in calendarArr" :key="item.value" @click="handleChangeCalendar(item)">
        <span :class="[
          item.isNow ? 'currentDay' : 'otherDay',
          { active: item.value === selectDate },
        ]">
          {{ item.value === todayFormatted ? '今' : item.label }}
        </span>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed, onMounted } from 'vue';
import moment from 'moment';
import { ArrowLeft, ArrowRight } from '@element-plus/icons-vue';


const weekData = ref(['日', '一', '二', '三', '四', '五', '六']);
const calendarArr = ref([]);
const currDate = ref("");
const selectDate = ref(moment().format('YYYY-MM-DD'));
const todayFormatted = ref(moment().format('YYYY-MM-DD'));
const year = ref<number>(new Date().getFullYear());
const month = ref<number>(new Date().getMonth() + 1);
const day = ref<number>(new Date().getDate());

const selectMonth = computed(() => {
  currDate.value = moment(selectDate.value).format('YYYY年MM月');
  return moment(selectDate.value).format('YYYY-MM');
});

const beginDay = computed(() => {
  let date = new Date(selectMonth.value + '-01');
  return date.getDay();
});

const curDays = computed(() => {
  let date = new Date(selectMonth.value + '-01');
  date.setMonth(date.getMonth() + 1, 0);
  return date.getDate();
});

const prevDays = computed(() => {
  let date = new Date(selectDate.value);
  date.setDate(0);
  return date.getDate();
});

onMounted(() => {
  getInitDate();
});

const handlePrev = () => {
  selectDate.value = moment(selectDate.value)
    .subtract(1, 'month')
    .format('YYYY-MM-DD');
  getInitDate();
};

const handleNext = () => {
  selectDate.value = moment(selectDate.value)
    .add(1, 'month')
    .format('YYYY-MM-DD');
  getInitDate();
};

const getInitDate = () => {
  let tempCalendarArr = new Array(35).fill(null);
  calendarArr.value = [];
  const date = new Date(selectDate.value);
  year.value = date.getFullYear();
  month.value = date.getUTCMonth() + 1;
  day.value = date.getDate();

  const yearStr = year.value.toString();
  const monthStr = month.value.toString().padStart(2, '0');

  for (let i = 1; i <= 35; i++) {
    if (i - beginDay.value > 0 && i - beginDay.value <= curDays.value) {
      const dayStr = (i - beginDay.value).toString().padStart(2, '0');
      tempCalendarArr[i] = {
        label: i - beginDay.value,
        value: `${yearStr}-${monthStr}-${dayStr}`,
        isNow: true,
      };
    } else if (i - beginDay.value <= 0) {
      let prevYear = year.value;
      let prevMonth = month.value - 1;
      if (month.value === 1) {
        prevYear = prevYear - 1;
        prevMonth = 12;
      }
      const prevYearStr = prevYear.toString();
      const prevMonthStr = prevMonth.toString().padStart(2, '0');
      const prevDayStr = (i - beginDay.value + prevDays.value).toString().padStart(2, '0');
      tempCalendarArr[i] = {
        label: i - beginDay.value + prevDays.value,
        value: `${prevYearStr}-${prevMonthStr}-${prevDayStr}`,
      };
    } else {
      let nextYear = year.value;
      let nextMonth = month.value + 1;
      if (month.value === 12) {
        nextYear = nextYear + 1;
        nextMonth = 1;
      }
      const nextYearStr = nextYear.toString();
      const nextMonthStr = nextMonth.toString().padStart(2, '0');
      const nextDayStr = (i - beginDay.value - curDays.value).toString().padStart(2, '0');
      tempCalendarArr[i] = {
        label: i - beginDay.value - curDays.value,
        value: `${nextYearStr}-${nextMonthStr}-${nextDayStr}`,
      };
    }
  }
  calendarArr.value = tempCalendarArr.filter((v) => v !== null);
};

const handleChangeCalendar = (item: { value: string }) => {
  selectDate.value = item.value;
  // emit('change-date', selectDate.value);
};
</script>

<style scoped lang="scss">
.canlendar-right {
  width: 210px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.btn-group {
  display: flex;
  width: 210px;
  height: 28px;
  align-items: center;
  justify-content: space-between;

  div {
    cursor: pointer;
  }
}

.week-header {
  color: #4e5969;
  font-size: 14px;
  text-align: center;
  width: 100%;
  display: flex;
  margin: 20px 0 9px 0;
}

.week-header div {
  flex: 1;
}

.week-day {
  width: 100%;
  /* border-right: 1px solid #e0e4ed;
    border-bottom: 1px solid #e0e4ed; */
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}

.day {
  height: 30px;
  width: 30px;
  text-align: center;
  line-height: 30px;
  /* border-left: 1px solid #e0e4ed;
    border-top: 1px solid #e0e4ed; */
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
}

.currentDay {
  color: #1d2129;
  font-size: 12px;
  display: inline-block;
  width: 24px;
  height: 24px;
  line-height: 24px;
  margin-top: 10px;
}

.otherDay {
  color: #c9cdd4;
  font-size: 12px;
  display: inline-block;
  width: 24px;
  height: 24px;
  line-height: 24px;
  margin-top: 10px;
}

.active {
  border-radius: 50%;
  background: #6698ff;
  color: #fff;
}

.task-height {
  width: 100%;
  height: 20px;
  background: rgba(233, 247, 255, 0.6);
  border-top: 2px solid rgba(24, 144, 255, 1);
  position: relative;
}

.task-height span {
  position: absolute;
  top: -39px;
  transform: translateX(-50%);
  display: inline-block;
  width: 100%;
  text-align: center;
  font-weight: 400;
  font-size: 12px;
  color: #5698d6;
  font-family: MiSans-Normal;
}

.over-load {
  background: rgba(255, 242, 232, 1);
  border-top: 2px solid rgba(255, 122, 69, 1);
}

.over-load span {
  color: #fa8c16;
}
</style>